import MainRoute from './routes/api.js'
import express from 'express'
import cors from 'cors'
import { pass_one_day } from './corn/index.js'
import { START_PORT } from './server.config.js'
import { createServer } from 'http'
import bodyParser from 'body-parser'
import history from 'connect-history-api-fallback'
import compression from 'compression'
import expressWs from 'express-ws'

const app = express()

// app.use(
//   history({
//     index: '/index.html',
//     // disableDotRule: true,
//     verbose: true,
//     htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'], // *!
//     rewrites: [{ from: /^\/api/, to: '/api' }],
//   }),
// )

//解析JSON 大小限制
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))
app.use(bodyParser.json({ limit: '50mb' }))

// gzip
app.use(compression({}))

// 跨域配置
app.use(
  cors({
    // origin: ['http://localhost:6069', 'http://127.0.0.1:6069'], // 限制IP
    origin: '*', // 本机调用
  }),
)

//托管静态路径
app.use(express.static('./static/web'))

// API
app.use('/api', MainRoute)

//创建服务 & 开启socket
const server = createServer(app)
expressWs(app, server)

// 启动服务
server.listen(START_PORT, () => {
  console.log(global.process.version, `运行成功: 127.0.0.1:${START_PORT}`)
  pass_one_day() //每天执行的任务
  global.cache = {}
  console.log(global.cache)
})
